//
//  ZIMKitCore+Group.swift
//  ZIMKit
//
//  Created by Kael Ding on 2023/1/9.
//

import Foundation
import ZIM

extension ZIMKitCore {
    
    /// 创建群聊
    /// - Parameters:
    ///   - groupName: 群名称
    ///   - groupID: 群 id
    ///   - inviteUserIDs: 群成员列表
    func createGroup(with groupName: String, groupID: String, inviteUserIDs: [String], callback: CreateGroupCallback? = nil) {
        let info = ZIMGroupInfo()
        info.groupName = groupName
        info.groupID = groupID
        zim?.createGroup(with: info, userIDs: inviteUserIDs, callback: { fullInfo, _, errorUserList, error in
            let info = ZIMKitGroupInfo(with: fullInfo)
            callback?(info, errorUserList, error)
        })
    }
    
    
    /// 加入群聊
    /// - Parameters:
    ///   - groupID: 群 id
    func joinGroup(by groupID: String, callback: JoinGroupCallback? = nil) {
        zim?.joinGroup(by: groupID, callback: { fullInfo, error in
            let info = ZIMKitGroupInfo(with: fullInfo)
            callback?(info, error)
        })
    }
    
    
    /// 退出群聊
    /// - Parameters:
    ///   - groupID: 群 id
    func leaveGroup(by groupID: String, callback: LeaveGroupCallback? = nil) {
        zim?.leaveGroup(by: groupID, callback: { groupID, error in
            callback?(error)
        })
    }
    
    
    /// 解散群聊,只有群主有这个功能
    /// - Parameters:
    ///   - groupID: 群 id
    ///   - callback: callback description
    func dismissGroup(by groupID: String, callback: DismissGroupCallback? = nil) {
        zim?.dismissGroup(by: groupID) { gId, error in
            callback?(gId, error)
        }
    }
    
    
    /// 邀请用户入群
    /// - Parameters:
    ///   - userIDs: 用户 id 列表
    ///   - groupID: 群 id
    func inviteUsersToJoinGroup(with userIDs: [String], groupID: String, callback: InviteUsersToJoinGroupCallback? = nil) {
        zim?.inviteUsersIntoGroup(with: userIDs, groupID: groupID, callback: { groupID, groupMemberInfos, errorUserInfos, error in
            let members = groupMemberInfos.compactMap { ZIMKitGroupMember(with: $0) }
            callback?(members, errorUserInfos, error)
        })
    }
    
    
    /// 获取群信息
    /// - Parameters:
    ///   - groupID: 群 id
    func queryGroupInfo(by groupID: String, callback: QueryGroupInfoCallback? = nil) {
        zim?.queryGroupInfo(by: groupID, callback: { fullInfo, error in
            let groupInfo = ZIMKitGroupInfo(with: fullInfo)
            callback?(groupInfo, error)
        })
    }
    
    
    /// 获取群成员信息
    /// - Parameters:
    ///   - userID: 用户 id
    ///   - groupID: 群 id
    func queryGroupMemberInfo(by userID: String, groupID: String, callback: QueryGroupMemberInfoCallback? = nil) {
        zim?.queryGroupMemberInfo(by: userID, groupID: groupID, callback: { _, zimMemberInfo, error in
            let groupMember = ZIMKitGroupMember(with: zimMemberInfo)
            self.groupMemberDict.add(groupID, member: groupMember)
            self.userDict[userID] = ZIMKitUser(userID: zimMemberInfo.userID, userName: zimMemberInfo.userName)
            callback?(groupMember, error)
        })
    }
    
    
    /// 获取群成员列表
    /// - Parameters:
    ///   - groupID: 群 id
    ///   - maxCount: 最大数量
    ///   - nextFlag: nextFlag description
    func queryGroupMemberListByGroupID(by groupID: String, maxCount: Int = 100, nextFlag: Int, callback: QueryGroupMemberListInfoCallback? = nil) {
        let config: ZIMGroupMemberQueryConfig = ZIMGroupMemberQueryConfig()
        config.count = UInt32(maxCount)
        config.nextFlag = UInt32(nextFlag)
        zim?.queryGroupMemberList(by: groupID, config: config, callback: { groupID,userList, nextFlag, errorInfo  in
            let memberList = userList.map { ZIMKitGroupMemberInfo(with: $0) }
            callback?(memberList, Int(nextFlag),errorInfo)
        })
    }
    
    
    /// 邀请人入群
    /// - Parameters:
    ///   - groupID: 群 id
    ///   - userIDs: 用户 id
    func inviteUsersIntoGroup(by groupID: String, userIDs: [String], callback: GroupUsersInvitedCallback? = nil) {
        zim?.inviteUsersIntoGroup(with: userIDs, groupID: groupID, callback: { groupID, userList, errorList, errorInfo in
            let memberList = userList.map { ZIMKitGroupMemberInfo(with: $0) }
            let errorMemberList = errorList.map { ZIMKitErrorUserInfo(with: $0) }
            callback?(groupID,memberList,errorMemberList,errorInfo)
        })
    }
    
    
    /// 移出群成员
    /// - Parameters:
    ///   - groupID: 群 id
    ///   - userIDs: 群成员 id, ["1"]
    func removeGroupMembers(by groupID: String, userIDs: [String], callback: GroupUsersRemoveCallback? = nil) {
        zim?.kickGroupMembers(by: userIDs, groupID: groupID) { groupID, userIds, errorUserList, errorInfo in
            let errorMemberList = errorUserList.map { ZIMKitErrorUserInfo(with: $0) }
            callback?(groupID, userIds, errorMemberList, errorInfo)
        }
    }
    
}
